package com.test.mylotterysystem.service.Impl;

import com.test.mylotterysystem.comment.errorCode.ServiceErrorCode;
import com.test.mylotterysystem.comment.exception.ServiceException;
import com.test.mylotterysystem.comment.utils.MyBeanUtil;
import com.test.mylotterysystem.controller.Param.CreatePrizeParam;
import com.test.mylotterysystem.controller.Param.PageParam;
import com.test.mylotterysystem.controller.Param.PrizeInfo;
import com.test.mylotterysystem.controller.Result.PrizeListResultDTO;
import com.test.mylotterysystem.dao.DO.PrizeDO;
import com.test.mylotterysystem.dao.Mapper.PrizeMapper;
import com.test.mylotterysystem.service.PrizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class PrizeServiceImpl implements PrizeService {
    @Autowired
    private PrizeMapper prizeMapper;

    @Value("${pic.local-path}")
    private String path;

    /**
     * 保存图片到本地
     *
     * @param multipartFile
     * @return
     */
    @Override
    public String savePicture(MultipartFile multipartFile) {
        if (multipartFile == null) {
            throw new ServiceException(ServiceErrorCode.PRIZE_PICTURE_IS_EMPTY);
        }

        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String name = UUID.randomUUID().toString().replace("-", "");

        try {
            multipartFile.transferTo(new File(path + "/" + (name + suffix)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return name + suffix;
    }

    /**
     * 创建奖品
     *
     * @param createPrizeParam
     * @param multipartFile
     * @return
     */
    @Override
    public Long create(CreatePrizeParam createPrizeParam, MultipartFile multipartFile) {
        if (createPrizeParam == null) {
            throw new ServiceException(ServiceErrorCode.PRIZE_PARAM_IS_EMPTY);
        }

        if (multipartFile == null) {
            throw new ServiceException(ServiceErrorCode.PRIZE_PICTURE_IS_EMPTY);
        }

        String imageUrl = savePicture(multipartFile);

        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setName(createPrizeParam.getPrizeName());
        prizeDO.setImageUrl(imageUrl);
        prizeDO.setPrice(createPrizeParam.getPrice());
        prizeDO.setDescription(createPrizeParam.getDescription());
        prizeMapper.create(prizeDO);

        return prizeDO.getId();
    }

    @Override
    public PrizeListResultDTO<PrizeInfo> getPrizeList(PageParam pageParam) {
        Integer total = prizeMapper.getCount();
        List<PrizeDO> prizeDOS = prizeMapper.getPrizeList(pageParam.offset(pageParam.getCurrentPage(), pageParam.getPageSize()), pageParam.getPageSize());

        List<PrizeInfo> prizeListResultDTO = MyBeanUtil.transOfPrizeListResultDTO(prizeDOS);

        PrizeListResultDTO<PrizeInfo> ret = new PrizeListResultDTO<>();
        ret.setTotal(total);
        ret.setRecords(prizeListResultDTO);
        return ret;
    }
}
